$(document).ready(function () {
    var str="";
        getDaily();
    function getDaily() {
        $.getJSON("/daily/getDaily", function (json) {
            $.each(json, function (i, item) {
                console.log("item.id"+item.id);
                $("#t1").append("<tr><td>" + item.id + "</td><td>" + item.name + "</td><td>" +
                    item.money+"</td><td>"+item.startTime+"</td><td>"+
                    item.stopTime+"</td><td>"+item.checkLocation+"</td><td>"+item.location+"</td><td>"+ "<input type='button' class='select' value='选择' /> "+ "</td></tr>");

            });
            $(".select").click(function(){
                $("#t2").append("<tr><td>"+$(this).parent().parent().find("td").eq(0).text()
                    +"</td><td>"+$(this).parent().parent().find("td").eq(1).text()+"</td><td>"+
                    "<input type='button' class='del' value='删除' /> " + "</td>></tr>");
                $(".del").click(function () {
                    $(this).parent().parent().remove();
                });

            });

        });
    }

  $("#insert").click(function () {
      var tableId = document.getElementById("t2");
      var trs = tableId.rows;
      var userId =$('#selectStudent option:selected') .val();
      console.log('userId:'+userId);
      //var userId = document.getElementById("selectStudent").value;
   //  var userId = $("#selectStudent").val();

      if (trs.length == 1) {
          alert("提交内容为空!!");
          return;
      }
      for(var i = 1, len = trs.length ; i < len; i++) {
          var id = trs[i].cells[0].innerText;
          str += id + ",";
      }

      $.ajax({
          type: "POST",
          url: "/userDaily/insertTo",
          dataType: "text",
          data: {

              userId:userId,
              dailyId:str,
          },
          success: function (data) {
              console.log(data);
              if (data==1) {
                  alert("添加成功!!!");}
              else{
                  alert ("添加失败!!!")
              }
              window.location.reload();
          }, error: function (xhr, textStatus) {
              console.log('错误');
              console.log(xhr);
              console.log(textStatus);
          }
      });




})

});




